package lu.med.praxiswahl.wahlumed.client.kalender;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemoteInt;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.service.beans.CustomerService;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;

/**
 * 
 * @author Lars Holger Roth
 */

public class NewPatientDialog extends JDialog implements ActionListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 8112546353637496134L;
    private JTextField mat, name1, name2, name3, vorname, tel;
    private KalenderPanel owner;
    private Main dataManager;

    public NewPatientDialog(Main dataManager, KalenderPanel owner)
    {
	super(dataManager, "Neue Patienten eintragen", true);
	this.owner = owner;
	this.dataManager = dataManager;
	this.setLayout(new BorderLayout());
	JPanel p = new JPanel();
	p.setLayout(new GridLayout(0, 2));
	mat = new JTextField();
	name1 = new JTextField();
	name2 = new JTextField();
	name3 = new JTextField();
	vorname = new JTextField();
	tel = new JTextField();
	p.add(new JLabel("Matricule:"));
	p.add(mat);
	p.add(new JLabel("Name:"));
	p.add(name1);
	p.add(new JLabel(""));
	p.add(name2);
	p.add(new JLabel(""));
	p.add(name3);
	p.add(new JLabel("Vorname:"));
	p.add(vorname);
	p.add(new JLabel("Telefonr.:"));
	p.add(tel);
	this.add(p, BorderLayout.CENTER);
	JPanel b = new JPanel();
	JButton ab, bb;
	ab = new JButton("Ok");
	bb = new JButton("Abbrechen");
	ab.addActionListener(this);
	bb.addActionListener(this);
	b.add(ab);
	b.add(bb);
	this.add(b, BorderLayout.SOUTH);
	this.setSize(new Dimension(200, 180));
	this.setLocation(300, 300);
	this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
	if (e.getActionCommand().equals("Ok"))
	{
	    if (mat.getText().equals(""))
	    {
		JOptionPane.showMessageDialog(dataManager,
			"Sie müssen zwingend eine Matricule eingeben");
		return;
	    }

	    CustomerServiceInt customer;

	    ContextFactoryRemoteInt remote_context = new ContextFactoryRemote();
	    customer = remote_context.getRemoteContextCustomerService();
	    //customer = new CustomerService();
	    customer.createCustomer(mat.getText());

	    String[] nn = new String[3];
	    nn[0] = name1.getText();
	    nn[1] = name2.getText();
	    nn[2] = name3.getText();

	    customer.setNachname(nn);
	    customer.setVorname(vorname.getText());
	    customer.setPrivatTel(tel.getText());

	    customer.save_customer();

	    // Speichert den neuen Patienten auf dem Server
	    /*
	     * try { String str = "ADD_PATIENT#" + pds.toServerString(); str +=
	     * "<~>" + ped.toString();
	     * dataManager.getOutputStream().writeUTF(str); String answer =
	     * dataManager.getInpupStream().readUTF(); if
	     * (!answer.equals("PATIENT_ADDED")) {
	     * JOptionPane.showMessageDialog(
	     * dataManager,"Der Server konnte die Patientin nicht hinzuf�gen"
	     * ); return; } } catch (IOException io) {
	     * JOptionPane.showMessageDialog
	     * (dataManager,"Verbindung zum Server unterbrochen"); return; }
	     */
	    // �bertr�gt die Daten in den Terminkalender
	    // Tr�gt den Termin ein
	    String name = name1.getText();
	    if (!name2.getText().equals(""))
	    {
		name += " " + name2.getText();
	    }
	    if (!name3.getText().equals(""))
	    {
		name += " " + name3.getText();
	    }
	    name += ", " + vorname.getText();

	    String telS = tel.getText();
	    if (telS.equals(""))
	    {
		telS = "N/A";
	    }
	    owner.insertAppointment(name, telS, mat.getText(), true);

	    this.setVisible(false);
	}
	else
	{
	    this.setVisible(false);
	}
    }
}